package com.newland.mesdk.demo.event;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.newland.me.SupportMSDAlgorithm;
import com.newland.mesdk.demo.main.R;
import com.newland.mtype.common.MESeriesConst;
import com.newland.mtype.event.DeviceEventListener;
import com.newland.mtype.module.common.emv.EmvTransController;
import com.newland.mtype.module.common.emv.EmvTransInfo;
import com.newland.mtype.module.common.emv.SecondIssuanceRequest;
import com.newland.mtype.module.common.pin.AccountInputType;
import com.newland.mtype.module.common.pin.K21PininutEvent;
import com.newland.mtype.module.common.pin.KeyManageType;
import com.newland.mtype.module.common.pin.PinConfirmType;
import com.newland.mtype.module.common.pin.PinInputEvent;
import com.newland.mtype.module.common.pin.PinInputResult;
import com.newland.mtype.module.common.pin.WorkingKey;
import com.newland.mtype.module.common.swiper.SwipResult;
import com.newland.mtype.module.common.swiper.SwiperReadModel;
import com.newland.mtype.tlv.TLVPackage;
import com.newland.mtype.util.Dump;
import com.newland.mtype.util.ISOUtils;
import com.newland.mesdk.demo.activity.BaseActivity;
import com.newland.mesdk.demo.interfaceimpl.K21PininputInterfaceImpl;
import com.newland.mesdk.demo.interfaceimpl.K21SwiperInterfaceImpl;
import com.newland.mesdk.demo.interfaceimpl.PinInputInterfaceImpl;
import com.newland.mesdk.demo.interfaceimpl.SwiperInterfaceImpl;
import com.newland.mesdk.demo.main.MainActivity;
import com.newland.mesdk.demo.main.MyApplication;
import com.newland.mesdk.demo.util.Const;
import com.newland.mesdk.demo.util.Const.MessageTag;

/**
 * Created by YJF on 2015/8/14 0014. Emv流程控制监听和QPBOC流程控制监听
 */
public class SimpleTransferListener implements TransferListenerInterface {
	private BaseActivity baseActivity;
	private static List L_55TAGS = new ArrayList();
	private String deviceType;
	private int inputLen = 0;
	private StringBuffer stringBuffer;
	private String pininputString;
	private Dialog pininput_dialog;
	static {
		L_55TAGS.add(0x9f26);
		L_55TAGS.add(0x9F27);
		L_55TAGS.add(0x9F10);
		L_55TAGS.add(0x9F37);
		L_55TAGS.add(0x9F36);
		L_55TAGS.add(0x95);
		L_55TAGS.add(0x9A);
		L_55TAGS.add(0x9C);
		L_55TAGS.add(0x9F02);
		L_55TAGS.add(0x5F2A);
		L_55TAGS.add(0x82);
		L_55TAGS.add(0x9F1A);
		L_55TAGS.add(0x9F03);
		L_55TAGS.add(0x9F33);
		L_55TAGS.add(0x9F74);
		L_55TAGS.add(0x9F34);
		L_55TAGS.add(0x9F35);
		L_55TAGS.add(0x9F1E);
		L_55TAGS.add(0x84);
		L_55TAGS.add(0x9F09);
		L_55TAGS.add(0x9F41);
		L_55TAGS.add(0x91);
		L_55TAGS.add(0x71);
		L_55TAGS.add(0x72);
		L_55TAGS.add(0xDF31);
		L_55TAGS.add(0x9F63);
		L_55TAGS.add(0x8A);
		L_55TAGS.add(0xDF32);
		L_55TAGS.add(0xDF33);
		L_55TAGS.add(0xDF34);
		L_55TAGS.add(0xDF75);
	}

	public SimpleTransferListener(BaseActivity baseActivity, String deviceType) {
		this.deviceType = deviceType;
		this.baseActivity = baseActivity;
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 2: // 键盘输入
				int len = (Integer) msg.obj;
				stringBuffer = new StringBuffer();
				for (int i = 0; i < len; i++) {
					stringBuffer.append(" * ");
				}
				baseActivity.getTvOperationMessage().setText("正在密码输入:" + stringBuffer.toString());
				break;

			default:
				break;
			}
		}
	};
	//	ExecuteRslt结果集：
	//	0x00/*成功,可能是脱机余额查询、明细成功或简易流程成功*/
	//	0x01/*交易授受*/
	//	0x02/*交易拒绝*/
	//	0x03/*联机*/
	//	0x0c/*成功获取EC余额*/
	//	0x0d/*非接触QPBOC交易接受*/
	//	0x0e/*非接触QPBOC交易拒绝*/
	//	0x0f/*非接触QPBOC交易联机*/
	//	0x10/*非接触MSD交易联机*/
	//	0x11/*成功获取QPBOC余额*/
	//	0xff/*交易失败*/
	//	0xfd/*FDDA失败*/
	//	0xfe/*FALLBACK*/
	//	0xfc/*取消*/
	//	0xfb/*交易金额大于终端限额*/
	//	0xfa/*卡片不支持电子现金*/
	@Override
	public void onQpbocFinished(EmvTransInfo context) {
		baseActivity.showMessage("qpboc交易结束:" + context.externalToString() + "\r\n", MessageTag.TIP);
		if (context.getExecuteRslt() == 0x02) {
			baseActivity.showMessage("交易失败：【交易拒绝】！" + "\r\n", MessageTag.TIP);
		} else if (context.getExecuteRslt() == 0x03) {
			baseActivity.showMessage("联机：【电子现金余额不足，请发起联机交易】！" + "\r\n", MessageTag.TIP);
			// todo 联机交易操作
		} else if (context.getExecuteRslt() == 0x00 || context.getExecuteRslt() == 0x01) {
			// 交易成功、交易授受
			baseActivity.showMessage(">>>>交易完成，卡号:" + context.getCardNo() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>交易完成，Serial number:" + context.getCardSequenceNumber() + "\r\n",
					MessageTag.DATA);
			baseActivity.showMessage("----8583 IC卡55域数据---表16　基本信息子域列表----" + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>应用密文(9f26):" + context.getAppCryptogram() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>密文信息数据(9F27):" + context.getCryptogramInformationData() + "\r\n",
					MessageTag.DATA);
			baseActivity.showMessage(">>>>发卡行应用数据(9F10):" + context.getIssuerApplicationData() + "\r\n",
					MessageTag.DATA);
			baseActivity.showMessage(">>>>不可预知数(9F37):" + context.getUnpredictableNumber() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>应用交易计数器(9F36):" + context.getAppTransactionCounter() + "\r\n",
					MessageTag.DATA);
			baseActivity.showMessage(">>>>终端验证结果(95):" + context.getTerminalVerificationResults() + "\r\n",
					MessageTag.DATA);
			baseActivity.showMessage(">>>>交易日期(9A):" + context.getTransactionDate() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>交易类型(9C):" + context.getTransactionType() + "\r\n", MessageTag.DATA);
			baseActivity
					.showMessage(">>>>授权金额(9F02):" + context.getAmountAuthorisedNumeric() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>交易货币代码(5F2A):" + context.getTransactionCurrencyCode() + "\r\n",
					MessageTag.DATA);
			baseActivity.showMessage(">>>>应用交互特征(82):" + context.getApplicationInterchangeProfile() + "\r\n",
					MessageTag.DATA);
			baseActivity.showMessage(">>>>终端国家代码(9F1A):" + context.getTerminalCountryCode() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>其它金额(9F03):" + context.getAmountOtherNumeric() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>终端性能(9F33):" + context.getTerminal_capabilities() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>电子现金发卡行授权码(9F74):" + context.getEcIssuerAuthorizationCode() + "\r\n",
					MessageTag.DATA);
			baseActivity.showMessage("----8583 IC卡55域数据---可选信息子域列表----" + "\r\n", MessageTag.TIP);
			baseActivity.showMessage(">>>>持卡人验证方法结果(9F34):" + context.getCvmRslt() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>终端类型(9F35):" + context.getTerminalType() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>接口设备序列号(9F1E):" + context.getInterface_device_serial_number() + "\r\n",
					MessageTag.DATA);
			baseActivity.showMessage(">>>>专用文件名称(84):" + context.getDedicatedFileName() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>软件版本号(9F09):" + context.getAppVersionNumberTerminal() + "\r\n",
					MessageTag.DATA);
			baseActivity.showMessage(">>>>交易序列计数器(9F41):" + context.getTransactionSequenceCounter() + "\r\n",
					MessageTag.DATA);
			baseActivity.showMessage(">>>>发卡行认证数据(91):" + context.getIssuerAuthenticationData() + "\r\n",
					MessageTag.DATA);
			baseActivity.showMessage(">>>>发卡行脚本1(71):" + context.getIssuerScriptTemplate1() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>发卡行脚本2(72):" + context.getIssuerScriptTemplate2() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>发卡方脚本结果(DF31):" + context.getScriptExecuteRslt() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>卡产品标识信息(9F63):" + context.getCardProductIdatification() + "\r\n",
					MessageTag.DATA);
			TLVPackage tlvPackage = context.setExternalInfoPackage(L_55TAGS);
			baseActivity.showMessage(">>>>55域打包集合:" + ISOUtils.hexString(tlvPackage.pack()) + "\r\n", MessageTag.DATA);
		} else {
			baseActivity.showMessage("错误的qpboc状态返回！" + context.getExecuteRslt() + "\r\n", MessageTag.DATA);
		}
		baseActivity.processingUnLock();
	}

	@Override
	public void onEmvFinished(boolean isSuccess, EmvTransInfo context) throws Exception {
		baseActivity.showMessage("emv交易结束:" + context.externalToString() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>交易完成，交易结果(DF75):" + context.getExecuteRslt() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>交易完成，卡号:" + context.getCardNo() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>交易完成，Serial number:" + context.getCardSequenceNumber() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage("----8583 IC卡55域数据---表16　基本信息子域列表----" + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>应用密文(9f26):" + context.getAppCryptogram() + "\r\n", MessageTag.DATA);
		baseActivity
				.showMessage(">>>>密文信息数据(9F27):" + context.getCryptogramInformationData() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>发卡行应用数据(9F10):" + context.getIssuerApplicationData() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>不可预知数(9F37):" + context.getUnpredictableNumber() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>应用交易计数器(9F36):" + context.getAppTransactionCounter() + "\r\n", MessageTag.DATA);
		baseActivity
				.showMessage(">>>>终端验证结果(95):" + context.getTerminalVerificationResults() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>交易日期(9A):" + context.getTransactionDate() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>交易类型(9C):" + context.getTransactionType() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>授权金额(9F02):" + context.getAmountAuthorisedNumeric() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>交易货币代码(5F2A):" + context.getTransactionCurrencyCode() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>应用交互特征(82):" + context.getApplicationInterchangeProfile() + "\r\n",
				MessageTag.DATA);
		baseActivity.showMessage(">>>>终端国家代码(9F1A):" + context.getTerminalCountryCode() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>其它金额(9F03):" + context.getAmountOtherNumeric() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>终端性能(9F33):" + context.getTerminal_capabilities() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>电子现金发卡行授权码(9F74):" + context.getEcIssuerAuthorizationCode() + "\r\n",
				MessageTag.DATA);
		baseActivity.showMessage("----8583 IC卡55域数据---可选信息子域列表----" + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>持卡人验证方法结果(9F34):" + context.getCvmRslt() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>终端类型(9F35):" + context.getTerminalType() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>接口设备序列号(9F1E):" + context.getInterface_device_serial_number() + "\r\n",
				MessageTag.DATA);
		baseActivity.showMessage(">>>>专用文件名称(84):" + context.getDedicatedFileName() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>软件版本号(9F09):" + context.getAppVersionNumberTerminal() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>交易序列计数器(9F41):" + context.getTransactionSequenceCounter() + "\r\n",
				MessageTag.DATA);
		baseActivity.showMessage(">>>>发卡行认证数据(91):" + context.getIssuerAuthenticationData() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>发卡行脚本1(71):" + context.getIssuerScriptTemplate1() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>发卡行脚本2(72):" + context.getIssuerScriptTemplate2() + "\r\n", MessageTag.DATA);
		baseActivity.showMessage(">>>>发卡方脚本结果(DF31):" + context.getScriptExecuteRslt() + "\r\n", MessageTag.DATA);
		baseActivity
				.showMessage(">>>>卡产品标识信息(9F63):" + context.getCardProductIdatification() + "\r\n", MessageTag.DATA);
		if (isSuccess) {
			TLVPackage tlvPackage = context.setExternalInfoPackage(L_55TAGS);
			((MyApplication) (baseActivity).getApplication()).setTlvPackage(tlvPackage);
			baseActivity.showMessage(">>>>55域打包集合:" + ISOUtils.hexString(tlvPackage.pack()) + "\r\n", MessageTag.DATA);
		}
		baseActivity.processingUnLock();
	}

	@Override
	public void onError(EmvTransController arg0, Exception arg1) {
		arg1.printStackTrace();
		baseActivity.showMessage("emv交易失败" + "\r\n", MessageTag.ERROR);
		baseActivity.showMessage(arg1.getMessage() + "\r\n", MessageTag.ERROR);
		baseActivity.processingUnLock();
	}

	@Override
	public void onFallback(EmvTransInfo arg0) throws Exception {
		baseActivity.showMessage("ic卡交易环境不满足:交易降级..." + "\r\n", MessageTag.ERROR);
		baseActivity.processingUnLock();
	}

	@Override
	public void onRequestOnline(EmvTransController controller, EmvTransInfo context) throws Exception {
		baseActivity.showMessage(">>>>交易完成，交易结果(DF75):" + context.getExecuteRslt() + "\r\n", MessageTag.DATA);
		TLVPackage tlvPackage = context.setExternalInfoPackage(L_55TAGS);
		baseActivity.showMessage(">>>>55域打包集合:" + ISOUtils.hexString(tlvPackage.pack()) + "\r\n", MessageTag.DATA);
		// 此处判断是开启度开启操作card_reader_flag=0，PBOC流程 card_reader_flag=1
		if (((MyApplication) baseActivity.getApplication()).getOpen_card_reader_flag() != 1) {
			baseActivity.showMessage("开启联机交易:" + context.externalToString() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>请求在线交易处理" + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(
					"终端验证结果(95):"
							+ (context.getTerminalVerificationResults() == null ? "无返回" : Dump.getHexDump(context
									.getTerminalVerificationResults())) + "\r\n", MessageTag.DATA);
			baseActivity
					.showMessage(
							"应用密文(9f26):"
									+ (context.getAppCryptogram() == null ? "无返回" : Dump.getHexDump(context
											.getAppCryptogram())) + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(
					"持卡人验证方法结果(9f34):" + (context.getCvmRslt() == null ? "无返回" : Dump.getHexDump(context.getCvmRslt()))
							+ "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>卡号:" + context.getCardNo() + "\r\n", MessageTag.DATA);
			baseActivity.showMessage(">>>>卡序列号:" + context.getCardSequenceNumber() + "\r\n", MessageTag.DATA);
			((MyApplication) (baseActivity).getApplication()).setIcCardNum(context.getCardNo());
			if (null != context.getTrack_2_eqv_data()) {
				baseActivity.showMessage(">>>>二磁道明文:" + ISOUtils.hexString(context.getTrack_2_eqv_data()) + "\r\n",
						MessageTag.DATA);
			}
			if (deviceType.equals(Const.DeviceName.ME30) || deviceType.equals(Const.DeviceName.ME31)) {
				SwiperInterfaceImpl swiperInterfaceImpl = new SwiperInterfaceImpl();
				SwipResult swipResult;
				try {

					swipResult = swiperInterfaceImpl.readEncryptResult(
							new SwiperReadModel[] { SwiperReadModel.READ_SECOND_TRACK }, new WorkingKey(
									Const.DataEncryptWKIndexConst.DEFAULT_TRACK_WK_INDEX), SupportMSDAlgorithm
									.getMSDAlgorithm(MESeriesConst.TrackEncryptAlgorithm.BY_UNIONPAY_MODEL));
					if (swipResult != null) {
						baseActivity.showMessage("请输入6位的密码..." + "\r\n", MessageTag.TIP);
						doPinInput(swipResult, controller);
					} else {
						baseActivity.showMessage("swipResult为空了！" + "\r\n", MessageTag.TIP);
					}

				} catch (Exception e) {
					e.printStackTrace();
					baseActivity.showMessage("onRequestOnline 异常：" + e + "\r\n", MessageTag.ERROR);
					baseActivity.processingUnLock();
				}
			}
			// todo !!!!!!!!!!从该处context中获取ic卡卡片信息后，发送银联8583交易
			SecondIssuanceRequest request = new SecondIssuanceRequest();
			request.setAuthorisationResponseCode("00");// 取自银联8583规范
														// 39域值,该参数按交易实际值填充
			controller.secondIssuance(request);
		}
	}

	@Override
	public void onRequestSelectApplication(EmvTransController arg0, EmvTransInfo arg1) throws Exception {
		baseActivity.showMessage("错误的事件返回，不可能要求应用选择！" + "\r\n", MessageTag.DATA);
		arg0.cancelEmv();
	}

	@Override
	public void onRequestTransferConfirm(EmvTransController controller, EmvTransInfo arg1) throws Exception {
		baseActivity.showMessage("交易确认完成" + "\r\n", MessageTag.DATA);
		controller.transferConfirm(true);
	}

	// IM81和N900会触发，ME30、ME31不会触发
	@Override
	public void onRequestPinEntry(final EmvTransController emvTransController, EmvTransInfo emvTransInfo)
			throws Exception {
		if (emvTransInfo.getCardNo() != null) {
			((MyApplication) (baseActivity).getApplication()).setIcCardNum(emvTransInfo.getCardNo());
		} else {
			baseActivity.showMessage("CardNo为空" + "\r\n", MessageTag.DATA);
		}

		K21SwiperInterfaceImpl k21SwiperInterfaceImpl = new K21SwiperInterfaceImpl();
		SwipResult swipRslt = k21SwiperInterfaceImpl.k21CalculateTrackData(emvTransInfo.getTrack_2_eqv_data(), null,
				new WorkingKey(Const.DataEncryptWKIndexConst.DEFAULT_TRACK_WK_INDEX),
				SupportMSDAlgorithm.getMSDAlgorithm(MESeriesConst.TrackEncryptAlgorithm.BY_LAKALA_KAIDIANBAO_MODEL));
		if (swipRslt != null) {
			baseActivity.showMessage("请输入6位的密码..." + "\r\n", MessageTag.DATA);
			doPinInput(swipRslt, emvTransController);
		} else {
			baseActivity.showMessage("swipResult为空了！" + "\r\n", MessageTag.DATA);
		}
	}

	// 输密码
	public void doPinInput(final SwipResult swipRslt, final EmvTransController emvTransController) throws Exception {
		if (deviceType.equals(Const.DeviceName.IM81)) {// IM81连接方式
			String acctHash = ((MyApplication) (baseActivity).getApplication()).getIcCardNum();
			K21PininputInterfaceImpl k21PinputInterfaceImpl = new K21PininputInterfaceImpl();
			k21PinputInterfaceImpl.startK21StandPininput("IM81开始输入密码", new WorkingKey(
					Const.PinWKIndexConst.DEFAULT_PIN_WK_INDEX), KeyManageType.MKSK, AccountInputType.USE_ACCOUNT,
					acctHash, 6, null, new byte[] { 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F' },
					PinConfirmType.ENABLE_ENTER_COMMANG, 600, TimeUnit.SECONDS, null, null,
					new DeviceEventListener<K21PininutEvent>() {
						@Override
						public void onEvent(K21PininutEvent event, Handler handler) {

							if (event == null) {
								inputLen = 0;
								baseActivity.showMessage("密码输入撤销" + "\r\n", MessageTag.DATA);
							} else if (event.isSuccess()) {
								inputLen = 0;
								baseActivity.showMessage("密码输入完成" + "\r\n", MessageTag.DATA);
								baseActivity.showMessage("ksn:" + Dump.getHexDump(event.getKsn()) + "\r\n",
										MessageTag.DATA);
								baseActivity.showMessage("密码:" + Dump.getHexDump(event.getEncrypPin()) + "\r\n",
										MessageTag.DATA);
								emvTransController.sendPinInputResult(event.getEncrypPin());
							} else if (event.isProcessing()) {
								PinInputEvent.NotifyStep notifyStep = event.getNotifyStep();
								if (notifyStep == PinInputEvent.NotifyStep.ENTER) {
									inputLen = inputLen + 1;
								} else if (notifyStep == PinInputEvent.NotifyStep.BACKSPACE) {
									inputLen = (inputLen <= 0 ? 0 : inputLen - 1);
									baseActivity.showMessage("按了退格键:" + inputLen, MessageTag.TIP);
								}
								Message msg = mHandler.obtainMessage(2);
								msg.obj = inputLen;
								msg.sendToTarget();
							} else if (event.isUserCanceled()) {
								inputLen = 0;
								baseActivity.showMessage("取消密码输入" + "\r\n", MessageTag.DATA);
							} else {
								inputLen = 0;
								baseActivity.showMessage("密码输入错误" + "\r\n", MessageTag.DATA);
							}
						}

						@Override
						public Handler getUIHandler() {
							return null;
						}
					});
		} else if (deviceType.equals(Const.DeviceName.ME30) || deviceType.equals(Const.DeviceName.ME31)) {
			PinInputInterfaceImpl pinInputInterfaceImpl = new PinInputInterfaceImpl();
			BigDecimal amt = ((MyApplication) baseActivity.getApplication()).getAmt();
			DecimalFormat df = new DecimalFormat("#.00");
			String msg = "消费金额为:" + df.format(amt) + "\n请输入6位交易密码:";
			PinInputEvent event = null;
			String acctHash = swipRslt.getAccount().getAcctHashId();
			event = pinInputInterfaceImpl.startStandardPinInput(new WorkingKey(
					Const.PinWKIndexConst.DEFAULT_PIN_WK_INDEX), KeyManageType.MKSK, AccountInputType.USE_ACCT_HASH,
					acctHash, 6, new byte[] { 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F' }, true, msg, 600,
					TimeUnit.SECONDS);
			if (event == null) {
				baseActivity.showMessage("密码输入撤销" + "\r\n", MessageTag.TIP);
				return;
			} else if (event.isSuccess()) {
				baseActivity.showMessage("密码输入完成" + "\r\n", MessageTag.TIP);
				((MyApplication) baseActivity.getApplication()).setPin(event.getEncrypPin());
				baseActivity.showMessage("ksn:" + Dump.getHexDump(event.getKsn()) + "\r\n", MessageTag.DATA);
				baseActivity.showMessage("密码:" + Dump.getHexDump(event.getEncrypPin()) + "\r\n", MessageTag.DATA);
			} else if (event.isUserCanceled()) {
				baseActivity.showMessage("密码输入撤销" + "\r\n", MessageTag.TIP);
			} else {
				baseActivity.showMessage("密码输入错误" + "\r\n", MessageTag.ERROR);
			}
		} else if (deviceType.equals(Const.DeviceName.N900)) {
			Message msg = MainActivity.getPinHandler().obtainMessage(0);
			msg.sendToTarget();
		} else if (deviceType.equals(Const.DeviceName.AUDIO)) {
			final PinInputInterfaceImpl pinInputInterfaceImpl = new PinInputInterfaceImpl();
			baseActivity.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					final AlertDialog.Builder builder = new AlertDialog.Builder(baseActivity);
					LayoutInflater inflater = LayoutInflater.from(baseActivity);
					final View view = inflater.inflate(R.layout.dialog_pininput, null);
					final EditText pininput_value = (EditText) view.findViewById(R.id.password);
					builder.setView(view);
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							pininputString = pininput_value.getText().toString();
							if (pininputString == null) {
								baseActivity.showMessage("输入密码为空!" + "\r\n", MessageTag.TIP);
								return;
							}
							try {
								PinInputResult result = null;
								result = pinInputInterfaceImpl.encryptPIN(new WorkingKey(
										Const.PinWKIndexConst.DEFAULT_PIN_WK_INDEX), KeyManageType.MKSK,
										AccountInputType.USE_ACCOUNT, swipRslt.getAccount().getAcctNo(), pininputString
												.getBytes());
								if (result != null) {
									baseActivity.showMessage("密码输入完成" + "\r\n", MessageTag.TIP);
									baseActivity.showMessage("ksn:" + Dump.getHexDump(result.getKSN()) + "\r\n",
											MessageTag.TIP);
									baseActivity.showMessage("密码:" + Dump.getHexDump(result.getPinblock()) + "\r\n",
											MessageTag.TIP);
								} else {
									baseActivity.showMessage("输入密码为空!" + "\r\n", MessageTag.TIP);
									baseActivity.processingUnLock();
								}
							} catch (Exception e) {
								baseActivity.showMessage("密码输入失败!" + e + "\r\n", MessageTag.ERROR);
							}
						}
					});
					builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							baseActivity.showMessage("输入密码为空!" + "\r\n", MessageTag.TIP);
							pininput_dialog.dismiss();
						}
					});
					pininput_dialog = builder.create();
					pininput_dialog.setTitle("密码输入");
					pininput_dialog.show();
					pininput_dialog.setCancelable(false);
					pininput_dialog.setCanceledOnTouchOutside(false);
				}
			});
		}
	}

}
